#version 130

out vec3 LightIntensity;
out vec3 Position;
out vec3 Normal;
out vec4 TexCoord;


uniform vec4 LightPosition; // Light position in eye coords.

uniform mat4 ModelMatrix;
uniform mat4 ViewMatrix;
uniform mat3 NormalMatrix;
uniform mat4 ProjectionMatrix;

varying vec3 tangentSurface2light;
varying vec3 tangentSurface2view;

void main()
{
	// Convert normal and position to eye coords
	vec3 tnorm = normalize( NormalMatrix * gl_Normal);
	//vec3 tnorm = normalize( NormalMatrix * normalize(gl_Vertex));
	vec4 eyeCoords = ViewMatrix * ModelMatrix * gl_Vertex;
	vec3 s = normalize(vec3(LightPosition - eyeCoords));
	
	mat4 MV =  ViewMatrix * ModelMatrix;
	// Convert position to clip coordinates and pass along
	gl_Position =  ProjectionMatrix * MV * gl_Vertex ;
	vec4 LightPosition2 = MV * LightPosition;

	// Pasamos las coordenadas de texturas a los FShaders
	TexCoord = gl_MultiTexCoord0;

	vec3 tangent;
    vec3 v1=cross(gl_Normal,vec3(0.0,0.0,-1.0));
    vec3 v2=cross(gl_Normal,vec3(0.0,-1.0,0.0));
    if(length(v1)>length(v2))
            tangent=v1;
    else
            tangent=v2;
    vec3 n=normalize(gl_NormalMatrix*gl_Normal);
    vec3 t=normalize(gl_NormalMatrix*tangent);
    vec3 b=cross(n,t);
	mat3 mat=mat3(t.x,b.x,n.x,t.y,b.y,n.y,t.z,b.z,n.z);

	vec3 vector=normalize(vec3(LightPosition) - vec3(gl_Position));
    tangentSurface2light=mat*vector;
       
    vector=normalize(-vec3(gl_Position));
    tangentSurface2view=mat*vector;

			
	
}